﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PT.Common {
    public class MathEx {
        /// <summary>Calculates the Greatest Common Divisor using Euclidean algorithm</summary>
        /// <param name="a">First number</param>
        /// <param name="b">Second number</param>
        /// <returns>Integer g, such that g | a and g | b. </returns>
        public static int GreatestCommonDivisor( int a, int b ) {
            int t;
            while ( b != 0 ) {
                t = b;
                b = a % b;
                a = t;
            }

            return Math.Abs(a);
        }

        /// <summary> Calculates the Least Common Multiple using the method derived from Euclidean GCD algorithm.
        /// The function employs the following formula: LCM( a, b ) = a * b / GCD( a, b ) </summary>
        /// <param name="a">First number</param>
        /// <param name="b">Second number</param>
        /// <returns>Smallest possible integer l, such that a | l and b | l.</returns>
        public static int LeastCommonMultiple( int a, int b ) {
            return a * b / GreatestCommonDivisor( a, b );
        }
    }
}
